package controllers

import (
	"encoding/json"
	"file/models"
	beego "github.com/beego/beego/v2/server/web"
	"strconv"
)

type FilmController struct {
	beego.Controller
}

func (c *FilmController) Add() {
	name := c.GetString("name")
	price, _ := c.GetFloat("price", 64)
	types := c.GetString("types")
	img := c.GetString("img")
	area := c.GetString("area")
	author := c.GetString("author")
	film := models.Film{
		Name:   name,
		Price:  price,
		Types:  types,
		Img:    img,
		Area:   area,
		Author: author,
	}
	if name == "" || price == 0 || types == "" || img == "" || area == "" || author == "" {
		c.Data["json"] = JsonRes{
			Code: 500,
			Msg:  "要添加的数据不能为空",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	err := models.DB.Model(&models.Film{}).Create(&film).Error
	if err != nil {
		c.Data["json"] = JsonRes{
			Code: 500,
			Msg:  "添加失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = JsonRes{
			Code: 200,
			Msg:  "添加成功",
			Data: nil,
		}
		c.ServeJSON()
	}
}
func (c *FilmController) Upload() {
	file, header, err := c.GetFile("img")
	if err != nil {
		c.Data["json"] = JsonRes{
			Code: 500,
			Msg:  "请上传文件",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	defer file.Close()
	path := "static/img/" + header.Filename
	c.SaveToFile("img", path)
	if header.Size > 5*1024*1024 {
		c.Data["json"] = JsonRes{
			Code: 500,
			Msg:  "文件大小不能超过5MB",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	if err != nil {
		c.Data["json"] = JsonRes{
			Code: 500,
			Msg:  "上传失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		imgUrl := "http://127.0.0.1:8080/" + path
		c.Data["json"] = JsonRes{
			Code: 200,
			Msg:  "上传成功",
			Data: imgUrl,
		}
		c.ServeJSON()
	}
}
func (c *FilmController) List() {
	page, _ := c.GetInt("page", 1)
	size, _ := c.GetInt("size", 10)
	name := c.GetString("name")
	query := models.DB.Model(&models.Film{})
	if name == "" {
		query = query.Where("name like ?", "%"+name+"%")
	}
	key := "film-" + strconv.Itoa(page)
	val, err := models.Rdb.Get(key).Result()
	pageData := make(map[string]interface{})
	if err != nil {
		var count int64
		err = query.Count(&count).Error
		offset := (page - 1) * size
		var film []models.Film
		err = query.Offset(offset).Limit(size).Find(&film).Error
		pageData["count"] = count
		pageData["film"] = film
		if err != nil {
			c.Data["json"] = JsonRes{
				Code: 500,
				Msg:  "查询失败",
				Data: nil,
			}
			c.ServeJSON()
			return
		} else {
			marshal, _ := json.Marshal(&pageData)
			value := string(marshal)
			models.Rdb.Set(key, value, 0)
			c.Data["json"] = JsonRes{
				Code: 200,
				Msg:  "查询成功-mysql",
				Data: pageData,
			}
			c.ServeJSON()
		}
	} else {
		json.Unmarshal([]byte(val), &pageData)
		c.Data["json"] = JsonRes{
			Code: 200,
			Msg:  "查询成功-redis",
			Data: pageData,
		}
		c.ServeJSON()
	}
}
func (c *FilmController) Info() {
	id, _ := c.GetInt("id")
	RankKey := "Rank"
	models.Rdb.ZIncrBy(RankKey, 1, strconv.Itoa(id))
	key := "film-" + strconv.Itoa(id)
	val, err := models.Rdb.Get(key).Result()
	var film models.Film
	if err != nil {

		err = models.DB.Model(&models.Film{}).Where("ID=?", id).First(&film).Error
		if err != nil {
			c.Data["json"] = JsonRes{
				Code: 500,
				Msg:  "详情查询失败",
				Data: nil,
			}
			c.ServeJSON()
			return
		} else {
			marshal, _ := json.Marshal(&film)
			value := string(marshal)
			models.Rdb.Set(key, value, 0)
			c.Data["json"] = JsonRes{
				Code: 200,
				Msg:  "详情查询成功-mysql",
				Data: film,
			}
			c.ServeJSON()
		}
	} else {
		json.Unmarshal([]byte(val), &film)
		c.Data["json"] = JsonRes{
			Code: 200,
			Msg:  "详情查询成功-redis",
			Data: film,
		}
		c.ServeJSON()
	}
}

/*func (c *FilmController) Rank() {
	key := "Rank"
	Rank := models.Rdb.ZRevRange(key, 1, 10-1)

}*/
